package cn.tedu.review;
/*售票案例*/
public class TestThread {
    public static void main(String[] args) {
        TicketThread t1=new TicketThread();
        TicketThread t2=new TicketThread();
        TicketThread t3=new TicketThread();
        TicketThread t4=new TicketThread();
        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }
}
class TicketThread extends Thread {
    /*将票数设置为静态,静态资源被本类所有对象共享,值只有一份,解决400张票的问题*/
    static int num = 100;

    @Override
    public void run() {
        while (true) {
            synchronized (TicketThread.class) {
                if (num > 0) {

                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(getName() + ":" + num--);
                }
                if (num <= 0) break;
            }


        }

    }


}